package versionControl.aspectj.library;

// When the lock is set - no versions are being changed
public abstract aspect Lock {
	
//	declare precedence: Lock, Update, Rollback, Commit;
	
	private boolean Versioned.isLocked = false;
	
	public boolean Versioned.isLocked()
	{
		return isLocked;
	}
	
	public void Versioned.setLocked(boolean lck)
	{
		isLocked = lck;
	}
	
	void around(Versioned obj) : set(* *.*) && target(obj) && withincode(* *.*(..)) 
	&& !within(versionControl.aspectj.library.*){
		int versionId = obj.getVersionID();
		if (obj.isLocked())
		{
			System.out.println("object is locked!");		
		}
		else 
		{
			proceed(obj);
		}
	}
	
	public abstract pointcut unLock(Versioned obj);
	public abstract pointcut lockObj(Versioned obj);
	
	void around(Versioned obj):lockObj(obj)
	{
		obj.setLocked(true);
		obj.setVersionID(-1);
	}
	
	void around(Versioned obj):unLock(obj)
	{
		obj.setLocked(false);
		obj.setVersionID(obj.getLatestVersionID());
	}
}
